var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];

var villageReg = /id=(\d+)\D+\s\(((\d+)\|(\d+))\)\sK/i;
var resSplitReg = /title="\w+"\D+\s*\d+/g;
var resourcesReg = /title="(\w+)"\D+\s*(\d+)/;
var buildingsReg = /((?:\w+ )*\w+) \<b\>\(Level (\d+)\)/;
var dateReg   = /(\w{3})\s+(\d{1,2}),\s+(\d{4})\s+(\d{2}):(\d{2}):(\d{2})/i;

var reportValues = null;
var config = null;

function setDistance(village) {
	var origin = game_data.village.coord.split("|");
	var dest = village.coord.split("|");
	var dx = dest[1] - origin[1];
	var dy = dest[0] - origin[0];
	village.distance = Math.sqrt(dx * dx + dy * dy);
}

function findMonth(t) {
	for(m in months)
		if(months[m] == t)
			return m;
}

function findBuildingByName(name) {
	var buildings = config.buildings.list;
	for(var i = 0; i < buildings.length; i++) {
		if(buildings[i].name == name) {
			return buildings[i];
		}
	}
}

function parseSent(text) {
	var date = dateReg.exec(text);
	if (date != null) {
		reportValues.time = new Date(date[3], findMonth(date[1]), date[2], date[4], date[5], date[6]).getTime();
	}
}

function parseDestination(text) {
	var villageValues = villageReg.exec(text);
	reportValues.destination.id = parseInt(villageValues[1]);
	reportValues.destination.coords = villageValues[2];
}

function parseDefender(text) {
	reportValues.barb = text == "---";
}

function parseScouted(text) {
	var scoutedValues = text.match(resSplitReg);
	if(scoutedValues) {
		for(var i = 0; i < scoutedValues.length; i++) {
			var values = scoutedValues[i].match(resourcesReg);
			if(values) {
				reportValues.scouted[values[1].toLowerCase()] = parseInt(values[2]);
			}
		}
	}
}

function parseBuildings(text) {
	var buildings = text.split('<br');
	for(var i = 0; i < buildings.length; i++) {
		var buildingLevel = buildings[i].match(buildingsReg);
		if(buildingLevel) {
			var building = findBuildingByName(buildingLevel[1]);
			if(building && reportValues.levels[building.id]) {
				reportValues.levels[building.id] = parseInt(buildingLevel[2]);
			}
		}
	}
}

function parseHaul(text) {
	var haulValues = text.match(resSplitReg);
	if(haulValues) {
		for(var i = 0; i < haulValues.length; i++) {
			var values = haulValues[i].match(resourcesReg);
			if(values) {
				reportValues.haul[values[1].toLowerCase()] = parseInt(values[2]);
			}
		}
	}
}

/* main statement */
function main() {
	reportValues = {barb:false,time:-1,haul:{wood:0,clay:0,iron:0},scouted:{wood:0,clay:0,iron:0},destination:{id:0,coords:""},levels:{wood:1,stone:1,iron:1,storage:1,wall:0}};
	var params = {"Sent":parseSent, "Destination:":parseDestination, "Defender:":parseDefender, "Resources scouted:":parseScouted, "Buildings:":parseBuildings, "Haul:":parseHaul};
	$('table tr').each(function(index) {
		var titleNode = $(this).children(":first");
		var parser = params[titleNode.text()];
		if(parser) {
			parser(titleNode.siblings().html());
		}
	});
	alert(JSON.stringify(reportValues));
}

function loadSettings() {
	config = new Config();
	config.onloaded = function() {
		main();
	};
	config.load();
}

function initSettings() {
	var configString = localStorage.getItem('twConfig');
	if(configString && configString.length > 0) {
		config = JSON.parse(configString);
	}
	if(config == null || !config.time || (new Date().getTime() - config.time) > 1000 * 3600 * 24 * 7) {
		$.getScript(scriptRoot+"settings.js", loadSettings);
	} else {
		main();
	}
}

initSettings();
